package com.example.springbootdemo;

import com.example.springbootdemo.aop.MyAopMainConfig;
import com.example.springbootdemo.aop.MyMathServes;
import com.example.springbootdemo.autowired.MyAutoConfigMainConfig;
import com.example.springbootdemo.autowired.MyDao;
import com.example.springbootdemo.ext.ExtConfig;
import com.example.springbootdemo.impot.MainConfig;
import com.example.springbootdemo.impot.Person;
import com.example.springbootdemo.demos.web.User;
import com.example.springbootdemo.impot.MyImportDemo;
import com.example.springbootdemo.lifecycle.Car;
import com.example.springbootdemo.lifecycle.MyConfigLifecycle;
import com.example.springbootdemo.property.MyPropertyMainConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

// @SpringBootApplication
public class SpringbootdemoApplication {

    @Test
    public void lifeCycle(){
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigLifecycle.class);
        System.out.println("----------------------ctx.getBeanNamesForType(Car.class)--------------------------");
        String[] car = ctx.getBeanNamesForType(Car.class);
        for (String s : car) {
            System.out.println(s);
        }

        System.out.println("----------------------ctx.getBeanDefinitionNames()--------------------------");
        // 获取bean 定义名称
        String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        ctx.close();
    }

    @Test
    public void impo() {
        // spring boot 启动方式
        //SpringApplication.run(SpringbootdemoApplication.class, args);

        // xml配置方式
        //ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");

        // 配置类方式
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);

        Person person = (Person)ctx.getBean("person");
        System.out.println(person);
        Person person2 = (Person)ctx.getBean("person");
        System.out.println(person == person2);

        // 根据类型获取 bean 名称
        System.out.println("----------------------ctx.getBeanNamesForType(User.class)--------------------------");
        String[] beanNamesForType = ctx.getBeanNamesForType(User.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        System.out.println("----------------------ctx.getBeanDefinitionNames()--------------------------");
        // 获取bean 定义名称
        String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        // 根据类型获取 bean 名称
        System.out.println("----------------------ctx.getBeanNamesForType(MyImportDemo.class)--------------------------");
        String[] myImportDemo = ctx.getBeanNamesForType(MyImportDemo.class);
        for (String s : myImportDemo) {
            System.out.println(s);
        }

        /*// 根据类型获取 bean 名称
        System.out.println("----------------------ctx.getBean(\"myFactoryBean\")--------------------------");
        // 获取 FactoryBean 创建的对象
        Object myImport = ctx.getBean("myFactoryBean");
        // 获取 FactoryBean 本身
        Object myFactoryBean = ctx.getBean("&myFactoryBean");
        System.out.println(myImport);
        System.out.println(myFactoryBean);*/

    }

    @Test
    public void property(){
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MyPropertyMainConfig.class);

        // 根据类型获取 bean 名称
        final com.example.springbootdemo.property.Person bean = ctx.getBean(com.example.springbootdemo.property.Person.class);
        System.out.println(bean);

        // context 方式获取
        final Environment environment = ctx.getEnvironment();
        String property = environment.getProperty("zs.name");
        System.out.println(property);

    }

    @Test
    public void autowired() {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.getEnvironment().setActiveProfiles("dev");
        ctx.register(MyAutoConfigMainConfig.class);

        System.out.println("----------------------ctx.getBeanNamesForType(Car.class)--------------------------");
        String[] car = ctx.getBeanNamesForType(MyDao.class);
        for (String s : car) {
            System.out.println(s);
        }

        Object myDao = ctx.getBean("myService");
        System.out.println(myDao);

    }

    @Test
    public void aop(){
        final AnnotationConfigApplicationContext ctx =  new AnnotationConfigApplicationContext(MyAopMainConfig.class);
        final MyMathServes myMathServes =(MyMathServes) ctx.getBean("myMathServes");
        System.out.println(myMathServes.div(2, 2));

    }

    @Test
    public void ext(){
        final AnnotationConfigApplicationContext ctx =  new AnnotationConfigApplicationContext(ExtConfig.class);


    }

    public static void main(String[] args) {

        System.out.println(testConnection("localhost",8300,2));

    }

    public static boolean testConnection(String host, int port, int timeout) {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(host, port), timeout);
            OutputStream outputStream = socket.getOutputStream();
            String msg ="ya8TGAAAAAAAAAABAAACfKztAAVzcgAxY29tLmFpLmFpZi5jc2YucHJvdG9jb2wuc29ja2V0Lm9iamVjdC5SZXF1ZXN0RGF0YVf6gw/YnwWWAgAETAAKYnVzaVBhcmFtc3QAD0xqYXZhL3V0aWwvTWFwO0wAC3NlcnZpY2VDb2RldAASTGphdmEvbGFuZy9TdHJpbmc7TAAJc3lzUGFyYW1zcQB+AAFMAAd2ZXJzaW9ucQB+AAJ4cHNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAx3CAAAABAAAAABdAALdmFyX3JlcXVlc3R1cgATW0xqYXZhLmxhbmcuT2JqZWN0O5DOWJ8QcylsAgAAeHAAAAABc3IAEWphdmEubGFuZy5JbnRlZ2VyEuKgpPeBhzgCAAFJAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAA54dAAxZ2draC1tYW5hZ2VzZXJ2aWNlX0xlYWRlckZlZWRiYWNrSW1wbF9nZXRGaWxlTmFtZXNxAH4ABD9AAAAAAAAMdwgAAAAQAAAAA3QAD3RpbWVvdXRCeUNsaWVudHNyAA5qYXZhLmxhbmcuTG9uZzuL5JDMjyPfAgABSgAFdmFsdWV4cQB+AAoAAAAAAAB1MHQAE2xvY2FsRm9yY2VOZXdUaHJlYWRzcgARamF2YS5sYW5nLkJvb2xlYW7NIHKA1Zz67gIAAVoABXZhbHVleHAAdAAEdXVpZHQAIDdmOWY2NmVlNTIzOTQ3N2Q4Y2I4MmM0MGFjZWRjNWFieHQAAzEuMA==";
            outputStream.write(base64ToByteArray(msg));

            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String re;
            while (( re = bufferedReader.readLine())!= null ){
                System.out.println(re);
            }
            bufferedReader.close();
            inputStream.close();
            outputStream.flush();
            outputStream.close();

            socket.close();
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    public static byte[] base64ToByteArray(String base64String) {
        if (base64String == null || base64String.isEmpty()) {
            return new byte[0]; // 返回空的字节数组以避免null
        }
        // 使用Java提供的Base64解码器对Base64字符串进行解码
        return Base64.getDecoder().decode(base64String);
    }
}
